package com.google.code.sip.ucenter.ws;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "status")
@XmlType(name = "WSResponseStatus")
@XmlAccessorType(XmlAccessType.NONE)
public class WSResponseStatus implements Comparable<WSResponseStatus> {
	public static final WSResponseStatus SUCCESS = new WSResponseStatus(0, null);
	public static final WSResponseStatus UNKNOW_REASON = new WSResponseStatus(
			1, "一个未知的错误发生");
	public static final WSResponseStatus SERVER_ERROR = new WSResponseStatus(2,
			"服务器发生了一个错误");
	public static final WSResponseStatus NOT_IMPLEMENTED = new WSResponseStatus(
			3, "服务未实现");
	public static final WSResponseStatus SERVICE_UNAVAILABLE = new WSResponseStatus(
			4, "服务临时不可用");
	public static final WSResponseStatus METHOD_UNAVAILABLE = new WSResponseStatus(
			5, "未知的方法");
	public static final WSResponseStatus REQUEST_EXCEED_LIMIT = new WSResponseStatus(
			6, "应用已达到设定的请求上限");
	public static final WSResponseStatus INVALID_IP = new WSResponseStatus(7,
			"请求来自未经授权的IP地址");
	public static final WSResponseStatus CALL_REST_API_EXCEED_LIMIT_ = new WSResponseStatus(
			8, "REST API调用次数超过了限制");
	public static final WSResponseStatus INVALID_API_KEY = new WSResponseStatus(
			100, "无效的API_KEY");
	public static final WSResponseStatus MUST_POST_METHOD = new WSResponseStatus(
			101, "必须是POST提交");

	@XmlElement
	private int code;
	@XmlElement
	private String message;

	/**
	 * Creates a new instance.
	 */
	public WSResponseStatus() {
	}

	/**
	 * Creates a new instance with the specified {@code code} and its {@code
	 * message}.
	 */
	public WSResponseStatus(int code, String message) {
		this.code = code;
		this.message = message;
	}

	/**
	 * Returns the code of this status.
	 */
	public int getCode() {
		return code;
	}

	/**
	 * Returns the reason phrase of this status.
	 */
	public String getMessage() {
		return message;
	}

	@Override
	public int hashCode() {
		return getCode();
	}

	@Override
	public boolean equals(Object o) {
		if (o instanceof WSResponseStatus) {
			return getCode() == ((WSResponseStatus) o).getCode();
		}
		return false;
	}

	@Override
	public int compareTo(WSResponseStatus o) {
		return getCode() - o.getCode();
	}

	@Override
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append(code);
		buf.append(' ');
		buf.append(message);
		return buf.toString();
	}
}
